﻿using DM_TOOLS.Tools.GPSConvert;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DM_TOOLS.Tools
{
    public static class DatafromCSVs
    {
       
        /// <summary>
        /// 此方法读取所有第一个csv和最后一个csv文件的ODO然后计算里程  返回-1说明没有一个csv的odo是可以用的，返回-2说明csv文件数量为0
        /// </summary>
        /// <param name="path"></param>
        /// <param name="monitortimes"></param>
        /// <param name="lasttime"></param>
        /// <returns></returns>
        public static async Task<double> GetTotalMileagesfromODO(this string path, string odo)
        {
            var filelist = path.GetExtensionFiles("csv");
            if(filelist.Length > 0)
            {
                double firstodo=1, lastodo=0; 
                //先从前面取csv
                for (int i = 0; i < filelist.Length; i++)
                {
                    var firstfile = filelist[i];
                    //获得csv的odo数据集
                    var firstlist = await firstfile.FullName.CSVFileSelectChanAsync(odo);
                    if (firstlist.Count > 0)
                    {
                        if (firstlist.First() > 0)
                        {
                            firstodo = firstlist.First();
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                //再从后面取csv
                for (int i = filelist.Length-1; i > 0; i--)
                {
                    var lastfile = filelist[i];
                    //获得csv的odo数据集
                    var lastlist = await lastfile.FullName.CSVFileSelectChanAsync(odo);
                    if (lastlist.Count > 0)
                    {
                        if (lastlist.First() > 0)
                        {
                            lastodo = lastlist.First();
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                double total = lastodo - firstodo;
                if (total >= 0)
                {
                    return Math.Round(total, 2);
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                return -2;
            }
        }
        /// <summary>
        /// 获取指定路径下所有csv文件里的经纬度数据，每个文件一行数据，最后还做了重采样，降到128个数据
        /// </summary>
        /// <param name="path"></param>
        /// <param name="monitortimes"></param>
        /// <returns></returns>
        public static async Task<List<List<double>>> GetTotalTracksfromCSVs(this string path, int monitortimes)
        {
            var filelist = path.GetExtensionFiles("csv");
            List<List<double>> results = new List<List<double>>();
            if (filelist.Length > 0)
            {
                List<double> lat = new List<double>();
                List<double> lon = new List<double>();
                for (int i = 0; i < filelist.Length; i++)
                {
                    var gpslist = await filelist[i].FullName.CSVFileSelectGpsAsync(monitortimes);
                    if (gpslist.Count == 2)
                    {
                        //lat.Add(gpslist[0]);
                        //lon.Add(gpslist[1]);

                        var newgps = GpsCoordinate.GpsToNewGps(gpslist[0], gpslist[1], 6);
                        lat.Add(newgps._wgLat);
                        lon.Add(newgps._wgLon);
                    }
                }
                //路径下所有文件数量都降采样到128个点
                if (lat.Count > 128)
                {
                    List<double> downsampledLat = new List<double>();
                    List<double> downsampledLon = new List<double>();
                    double step = (double)lat.Count / 128;
                    for (int i = 0; i < 128; i++)
                    {
                        int index = (int)(i * step);
                        downsampledLat.Add(lat[index]);
                        downsampledLon.Add(lon[index]);
                    }
                    results.Add(downsampledLat);
                    results.Add(downsampledLon);
                }
                else
                {
                    results.Add(lat);
                    results.Add(lon);
                }

            }
            return results;
        }

        /// <summary>
        /// 获取指定路径下所有csv文件里的经纬度数据，先降到128个文件，然后再读取数据，每一个文件一行数据，这样会比旧版的快
        /// </summary>
        /// <param name="path"></param>
        /// <param name="monitortimes"></param>
        /// <returns></returns>
        public static async Task<List<List<double>>> GetTotalTracksfromCSVsV2(this string path, int monitortimes)
        {
            var filelist = path.GetExtensionFiles("csv");
            List<List<double>> results = new List<List<double>>();


            if (filelist.Length > 0)
            {
                List<double> lat = new List<double>();
                List<double> lon = new List<double>();
                if (filelist.Length > 128)
                {
                    double filestep = (double)filelist.Length / 128;

                    //先降到128个文件后再读取
                    for (int i = 0; i < 128; i++)
                    {
                        int index = (int)(i * filestep);
                        var gpslist = await filelist[index].FullName.CSVFileSelectGpsAsync(monitortimes);
                        if (gpslist.Count == 2)
                        {
                            var newgps = GpsCoordinate.GpsToNewGps(gpslist[0], gpslist[1], 6);
                            lat.Add(newgps._wgLat);
                            lon.Add(newgps._wgLon);
                        }
                    }
                    results.Add(lat);
                    results.Add(lon);
                }
                else
                {
                    for (int i = 0; i < filelist.Length; i++)
                    {
                        var gpslist = await filelist[i].FullName.CSVFileSelectGpsAsync(monitortimes);
                        if (gpslist.Count == 2)
                        {
                            var newgps = GpsCoordinate.GpsToNewGps(gpslist[0], gpslist[1], 6);
                            lat.Add(newgps._wgLat);
                            lon.Add(newgps._wgLon);
                        }
                    }
                    results.Add(lat);
                    results.Add(lon);
                }


            }
            return results;
        }
    }
}
